package com.example.learnandroidprofiler;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class CpuProfilerActivity extends AppCompatActivity {

    private Button toggleJobBtn;
    private ProgressBar demoPb;

    private boolean threadRun;
    private int pbCurrent;

    private UIHandler mUiHandler;

    private final int UPDATE_HANDLER_KEY = 0x00;
    private final int PB_MAX = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cpu_profiler);
        findView();
        initView();
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void findView() {
        toggleJobBtn = findViewById(R.id.activity_cpu_profiler_toggle_job_btn);
        demoPb = findViewById(R.id.activity_cpu_profiler_demo_pb);
    }

    private void initView() {
        getSupportActionBar().setTitle(getResources().getString(R.string.activity_cpu_profiler_title));
        toggleJobBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                togglePbRun();
            }
        });
    }

    private void togglePbRun() {
        toggleJobBtn.setEnabled(false);
        threadRun = true;
        pbCurrent = 0;
        demoPb.setProgress(pbCurrent);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (threadRun) {
                    try {
                        Thread.sleep(1000 / 60);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (pbCurrent > PB_MAX) {
                        pbCurrent = 0;
                    } else {
                        pbCurrent++;
                    }
                    Message msg = new Message();
                    msg.what = UPDATE_HANDLER_KEY;
                    mUiHandler.sendMessage(msg);
                }
            }
        }).start();
    }

    private void initData() {
        initJobA(true);
        initJobC();
        mUiHandler = new UIHandler();
    }

    private void initJobA(boolean ifNeedJobB) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (ifNeedJobB) {
            initJobB();
        }
    }

    private void initJobB() {
        try {
            Thread.sleep(250);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void initJobC() {
        initJobA(true);
        initJobA(true);
        initJobA(false);
    }

    class UIHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case UPDATE_HANDLER_KEY:
                    demoPb.setProgress(pbCurrent);
                    break;
            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        threadRun = false;
    }
}